#include <stdio.h>
#include <string.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include "ddebug.h"
#include "global_data.h"
#include "message_recv.h"
#include "recvmsg_type.h"
#include "shmctl.h"
#include "zigbee.h"
#include "gprs.h"
#include "sql_ctl.h"




void *message_recv(void *argc) 
{ 
	key_t key/*,key2*/;
	int msgid;
	int fd_zigbee = 0;
	struct msg msg_buf;
	memset(&msg_buf,0,sizeof(msg_buf));

	if((key = ftok("/app", 'g')) < 0)
	{
		syserr("ftok");
		exit(1);
	}
	DDEBUG(P_NORMAL,"<<MESSAGE>> get key(/app,g):%x\n",key);

	if((msgid = msgget(key, 0666|IPC_CREAT)) < 0)
	{
		syserr("msgget");
		exit(1);
	}

	struct shm_addr *shm_buf = NULL;//(struct shm_addr *)malloc(sizeof(struct shm_addr));


	DDEBUG(P_NORMAL,"<<MESSAGE>> message_recv is work!\n");
	while(1)
	{
		msgrcv(msgid, &msg_buf,sizeof(msg_buf)-sizeof(long),1,0);
		DDEBUG(P_NORMAL,"<<MESSAGE>> \n--->type: %ld ,msgtype : %ld<----\n",msg_buf.type, msg_buf.msgtype);
		
		
		if(msg_buf.type == 1 && msg_buf.msgtype == 4)//m0_ctl
		{

			send_msg_t send_buf = {0};
			send_buf.machine_num = ((msg_buf.text[0]>>6) & (0xf));
			send_buf.msg_type = 3;
			
			DDEBUG(P_NORMAL,"<<MESSAGE>> In the sto[%d]:\n",((msg_buf.text[0]>>6) & (0xf)));
			switch((msg_buf.text[0]>>4)&0x3)
			{
			case 0:
				if((msg_buf.text[0]&0xf) == 0)
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> switch off the fan!\n");
					send_buf.data.commond.id = 2;
					send_buf.data.commond.operation = 2;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				else
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> fan is on ,control fan:%d\n",(msg_buf.text[0]&0xf));
					send_buf.data.commond.id = 2;
					send_buf.data.commond.operation = 1;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				break;
			case 1:
				DDEBUG(P_NORMAL,"<<MESSAGE>> beep is control:%d\n",msg_buf.text[0]&0xf);
				if((msg_buf.text[0]&0xf) == 0)
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> switch off the beep!\n");
					send_buf.data.commond.id = 3;
					send_buf.data.commond.operation = 2;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				else
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> beep is on ,control beep:%d\n",(msg_buf.text[0]&0xf));
					send_buf.data.commond.id = 3;
					send_buf.data.commond.operation = 1;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
					
				break;
			case 2:
				DDEBUG(P_NORMAL,"<<MESSAGE>> led is control:%d\n",msg_buf.text[0]&0xf);
				if((msg_buf.text[0]&0xf) == 0)
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> switch off the led!\n");
					send_buf.data.commond.id = 1;
					send_buf.data.commond.operation = 2;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				else
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> led is on ,con led:%d\n",(msg_buf.text[0]&0xf));
					send_buf.data.commond.id = 1;
					send_buf.data.commond.operation = 1;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				break;
			case 3:
				if((msg_buf.text[0]&0xf) == 0xf)
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> switch off the led-seg!\n");
					send_buf.data.commond.id = 4;
					send_buf.data.commond.operation = 0xff;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				else
				{
					DDEBUG(P_NORMAL,"<<MESSAGE>> leg-seg is on ,show number:%d\n",(msg_buf.text[0]&0xf));
					send_buf.data.commond.id = 4;
					send_buf.data.commond.operation = msg_buf.text[0] & 0xf;
					write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));
					DDEBUG(P_NORMAL,"<<MESSAGE>> send ma:%d, ty:%d,id:%d,op:%d!\n",send_buf.machine_num,send_buf.msg_type,send_buf.data.commond.id,send_buf.data.commond.operation);
				}
				break;
			default:
				DDEBUG(P_NORMAL,"<<MESSAGE>> Error\n");

			}
		}	
	
		if(msg_buf.type == 1 && msg_buf.msgtype == 5)//setEnv
		{
			struct setEnv new = {0};
			int pra_strno;
			pra_strno = msg_buf.text[0] - '0';
			DDEBUG(P_NORMAL,"<<MESSAGE>> In the Storage[%d]\n",pra_strno);


			memcpy (&new,msg_buf.text+1, 24);
			send_msg_t send_buf = {0};
			send_buf.machine_num = pra_strno;
			send_buf.msg_type = 5;
			send_buf.data.limit.tem_max.htem = new.temMAX;
			send_buf.data.limit.tem_max.ltem = 0;
			send_buf.data.limit.tem_min.htem = new.temMIN;
			send_buf.data.limit.tem_min.ltem = 0;

			send_buf.data.limit.hum_max.hhum = new.humMAX;
			send_buf.data.limit.hum_max.lhum = 0;
			send_buf.data.limit.hum_min.hhum = new.humMIN;
			send_buf.data.limit.hum_min.lhum = 0;

			
			send_buf.data.limit.light_max.light = new.illMAX;
			send_buf.data.limit.light_min.light = new.illMIN;

			write_zigbee_a9(fd_zigbee,(char *)&send_buf,sizeof(send_msg_t));

			sql_write_limit(pra_strno, &(send_buf.data.limit));
			
			DDEBUG(P_NORMAL,"<<MESSAGE>> temMAX:%d\n",new.temMAX);
			DDEBUG(P_NORMAL,"<<MESSAGE>> temMIN:%d\n",new.temMIN);
			DDEBUG(P_NORMAL,"<<MESSAGE>> humMAX:%d\n",new.humMAX);
			DDEBUG(P_NORMAL,"<<MESSAGE>> humMIN:%d\n",new.humMIN);
			DDEBUG(P_NORMAL,"<<MESSAGE>> illMAX:%d\n",new.illMAX);
			DDEBUG(P_NORMAL,"<<MESSAGE>> illMIN:%d\n",new.illMIN);

			shmget_env(&shm_buf);
			
			shm_buf->rt_status.storage_no[pra_strno].temperatureMAX = new.temMAX;
			shm_buf->rt_status.storage_no[pra_strno].temperatureMIN = new.temMIN;
			shm_buf->rt_status.storage_no[pra_strno].humidityMAX = new.humMAX;
			shm_buf->rt_status.storage_no[pra_strno].humidityMIN = new.humMIN;
			shm_buf->rt_status.storage_no[pra_strno].illuminationMAX = new.illMAX;
			shm_buf->rt_status.storage_no[pra_strno].illuminationMIN = new.illMIN;

			shmset_env(shm_buf);

		}

		if(msg_buf.msgtype == 10)//setsms
		{
			DDEBUG(P_NORMAL,"<<MESSAGE>> to    number :%s\n",msg_buf.text);
			DDEBUG(P_NORMAL,"<<MESSAGE>> center number:%s\n",msg_buf.text+12);
			set_phone_number(msg_buf.text);
		}
		if(msg_buf.msgtype == 11)//wifi
		{

			DDEBUG(P_NORMAL,"<<MESSAGE>> gateway :%s\n",msg_buf.text);
			DDEBUG(P_NORMAL,"<<MESSAGE>>    ip   :%s\n",msg_buf.text+15);
		}
		if(msg_buf.msgtype == 3)//capture
		{

			DDEBUG(P_NORMAL,"<<MESSAGE>> get picture :%d\n",msg_buf.text[0]);
		}
		if(msg_buf.msgtype == 12)//clear warning
		{

			shmget_env(&shm_buf);
			shm_buf->rt_status.storage_no[msg_buf.text[0] - '0'].warningMask = 0;

			shmset_env(shm_buf);
			DDEBUG(P_NORMAL,"<<MESSAGE>> clear sto:%d warningMask :%d\n",msg_buf.text[0] - '0',shm_buf->rt_status.storage_no[((msg_buf.text[0]>>6) & (0xf))].warningMask);
		}
	}
} 





